package com.luych.toolbox.common.feign.view.gateway.center;

import com.luych.toolbox.common.enums.MicroInstanceStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("微服务实例")
public class InstanceView {

    @ApiModelProperty("实例ID")
    private String instanceId;
    @ApiModelProperty("主机名")
    private String hostName;
    @ApiModelProperty("IP地址")
    private String ipAddr;
    @ApiModelProperty("HTTP端口")
    private Integer httpPort;
    @ApiModelProperty("HTTPS端口")
    private Integer httpsPort;
    @ApiModelProperty("状态")
    private MicroInstanceStatus status;
    @ApiModelProperty("启动时间")
    private Long upTimestamp;

    public String getInstanceId() {
        return instanceId;
    }

    public void setInstanceId(String instanceId) {
        this.instanceId = instanceId;
    }

    public String getHostName() {
        return hostName;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    public String getIpAddr() {
        return ipAddr;
    }

    public void setIpAddr(String ipAddr) {
        this.ipAddr = ipAddr;
    }

    public Integer getHttpPort() {
        return httpPort;
    }

    public void setHttpPort(Integer httpPort) {
        this.httpPort = httpPort;
    }

    public Integer getHttpsPort() {
        return httpsPort;
    }

    public void setHttpsPort(Integer httpsPort) {
        this.httpsPort = httpsPort;
    }

    public MicroInstanceStatus getStatus() {
        return status;
    }

    public String getStatusValue() {
        return status == null ? null : status.getValue();
    }

    public void setStatus(MicroInstanceStatus status) {
        this.status = status;
    }

    public Long getUpTimestamp() {
        return upTimestamp;
    }

    public void setUpTimestamp(Long upTimestamp) {
        this.upTimestamp = upTimestamp;
    }

    public String getUpTimeDuration() {
        if (upTimestamp == null) {
            return null;
        }
        long duration = System.currentTimeMillis() - upTimestamp;
        if (duration < 1000) {
            return "<1秒";
        } else if (duration < 60000) {
            return duration / 1000 + "秒";
        } else if (duration < 3600000) {
            return duration / 60000 + "分钟";
        } else if (duration < 86400000) {
            return duration / 3600000 + "小时";
        } else {
            return duration / 86400000 + "天" + duration % 86400000 / 3600000 + "小时";
        }
    }
}
